package com.lightjet.macross.base.feign.api.controller;

import com.lightjet.macross.base.feign.api.domain.AuthUser;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.ModelAttribute;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public abstract class PrincipalController {

    @ModelAttribute("CurrentUser")
    public AuthUser GetCurrentUser(HttpServletRequest request, HttpServletResponse response)
    {

        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if(authentication.getPrincipal() instanceof AuthUser)
        {
            return (AuthUser)authentication.getPrincipal();
        }
        else
        {
            AuthUser authUser = new AuthUser();
            authUser.setUsername("Guest");
            authUser.setDisplayname("可信第三方访客");
            authUser.setId("-1");
            return authUser;
        }
    }
}
